<?php

/**
 * This is the model class for table "user_quiz_attempt".
 *
 * The followings are the available columns in table 'user_quiz_attempt':
 * @property integer $id
 * @property integer $user_id
 * @property integer $quiz_id
 * @property integer $level
 * @property integer $no_of_questions
 * @property integer $attempted_questions
 * @property integer $current_question
 * @property integer $total_points
 * @property string $status
 * @property string $result
 * @property string $created
 * @property string $end_time
 * @property string $modified
 */
class UserQuizAttempt extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserQuizAttempt the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_quiz_attempt';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, quiz_id, level, no_of_questions, attempted_questions, current_question, total_points, status, result', 'required'),
			array('user_id, quiz_id, level, no_of_questions, attempted_questions, current_question, total_points', 'numerical', 'integerOnly'=>true),
			array('status', 'length', 'max'=>10),
			array('result', 'length', 'max'=>4),
			array('modified','default','value'=>new CDbExpression('NOW()'),'setOnEmpty'=>false,'on'=>'update'),
			array('created,modified','default','value'=>new CDbExpression('NOW()'),'setOnEmpty'=>false,'on'=>'insert'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, quiz_id, level, no_of_questions, attempted_questions, current_question, total_points, status, result, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'quiz_id' => 'Quiz',
			'level' => 'Level',
			'no_of_questions' => 'No Of Questions',
			'attempted_questions' => 'Attempted Questions',
			'current_question' => 'Current Question',
			'total_points' => 'Total Points',
			'status' => 'Status',
			'end_time'=>'end_time',
			'result' => 'Result',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
     	$criteria=new CDbCriteria;
      $criteria->compare('id',$this->id);
     if($_SESSION['role_id'] =="1")
		$criteria->compare('user_id',$this->user_id);
    else                                                                          
     $criteria->compare('user_id',Yii::app()->user->id);
		$criteria->compare('quiz_id',$this->quiz_id);
		$criteria->compare('level',$this->level);
		$criteria->compare('no_of_questions',$this->no_of_questions);
		$criteria->compare('attempted_questions',$this->attempted_questions);
		$criteria->compare('current_question',$this->current_question);
		$criteria->compare('total_points',$this->total_points);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('result',$this->result,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
    $criteria->order = 'created DESC';
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
  
	//get list of model objects of specific type
	public static function getList($type, $arrParams=array())
	{
		switch ($type)
		{
			case "GRADE-LIST-BY-ORGANISATION-ID":
				$gradeList = Grade::model()->findAll(
					'organisation_id=?',array($arrParams['orgid'])
					);
				return $gradeList;
				break;
		} 
	}
		//get  model object of specific type
	public static function getModel($type, $arrParams) 
	{
		switch($type)
		{
			
			case "USER-QUIZ-ATTEMPT":
				$attempt = UserQuizAttempt::model()->find(
					'quiz_id=? and user_id=?',array($arrParams['quizid'],$arrParams['userid'])
					);
          
				return $attempt;
			break;
			
			case "USER-QUIZ-ATTEMPT-BYMAXTOTAL":
				//echo 'SELECT id,total_points FROM user_quiz_attempt where quiz_id = "'.$arrParams['quizid'].'" AND user_id = "'.$arrParams['userid'].'" AND status="completed" ORDER BY total_points DESC,created ASC LIMIT 0,1';exit;
				$attempt	=	Yii::app()->db->createCommand('SELECT id,total_points FROM user_quiz_attempt where quiz_id = "'.$arrParams['quizid'].'" AND user_id = "'.$arrParams['userid'].'" AND status="completed" ORDER BY total_points DESC,created ASC LIMIT 0,1')->queryRow();
				//echo "<pre>";print_r($attempt);exit;
				/*$attempt = UserQuizAttempt::model()->find(
					'quiz_id=? and user_id=?',array($arrParams['quizid'],$arrParams['userid'])
					);*/
				return $attempt;
				break;
			
			case "USER-QUIZ-ATTEMPT-INPROGRESS":
				$attempt = UserQuizAttempt::model()->find(
					'quiz_id=? and user_id=? and level=? and status="inprogress"',array($arrParams['quizid'],$arrParams['userid'],$arrParams['level'])
					);
				return $attempt;
			break;
			
			case "ATTEMPT-ID":
				$attempt = UserQuizAttempt::model()->findByPk((int)$arrParams['attemptid']
					);
				return $attempt;
			break;
      
	     
		
		}
	}
}